package edu.whu.irlab.woscrawl.doi;

import edu.whu.irlab.woscrawl.util.WebDriverFactory;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static edu.whu.irlab.woscrawl.util.HttpDownloader.download;

/**
 * Created by zhongwei on 2016/12/31.
 */
public class DOI {

    static void getDOI(File outputDir) throws IOException {
        Set<String> doneIDs = new HashSet<String>();
        File[] files = outputDir.listFiles();
        for (File file : files) {
            doneIDs.add(file.getName());
        }
        String content = FileUtils.readFileToString(new File("records.txt"));
        String[] arr = content.split("==========");
        for (String str : arr) {
            String[] lines = str.split("\r\n");
            int lineNums = lines.length;
            String id = "";
            if (lines[0].trim().length() == 0)
                id = lines[1] + "txt";
            else id = lines[0] + "txt";
            if (doneIDs.contains(id))
                continue;
            System.out.println(id);
            File file = new File(outputDir, id);
            FileWriter fileWriter = new FileWriter(file);
            String url = lines[lineNums - 1];
            String detail = download(url);
            Document document = Jsoup.parse(detail);
            try {
                Element element = document.getElementsByClass("l-content").get(0);
                fileWriter.write(element.html());
                fileWriter.flush();
                fileWriter.close();
            } catch (Exception ex) {
                file.delete();
                fileWriter.flush();
                fileWriter.close();
                createNewSession();
                getDOI(outputDir);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        File file = new File("doiDir");
        getDOI(file);
//        System.out.println(download("http://apps.webofknowledge.com/full_record.do?product=UA&search_mode=GeneralSearch&qid=1&SID=2A5193xgtEiILshmlif&page=1&doc=1"));
//        createNewSession();
    }

    static void createNewSession() throws IOException {
        WebDriver driver = WebDriverFactory.getWebDriver();
        search(driver);
        while (true) {
            try {
                WebElement aEle = driver.findElements(By.className("smallV110")).get(0);
                String href = aEle.getAttribute("href");
                Pattern pattern = Pattern.compile("&qid=\\d{1,3}&SID=.+?&");
                Matcher matcher = pattern.matcher(href);
                String replacement = "";
                if (matcher.find()) {
                    replacement = matcher.group(0);
                }
                String content = FileUtils.readFileToString(new File("records.txt"));
                content = content.replaceAll("&qid=\\d{1,3}&SID=.+?&", replacement);
                FileWriter fileWriter = new FileWriter(new File("records.txt"));
                fileWriter.write(content);
                fileWriter.flush();
                fileWriter.close();
                driver.quit();
                break;
            } catch (Exception e) {

            }
        }
    }
    public static void search(WebDriver driver) {
        driver.get("http://apps.webofknowledge.com/UA_GeneralSearch_input.do?product=UA&search_mode=GeneralSearch&SID=U2FP8cb3RroGapsH1na&preferencesSaved=");
        while (true) {
            try {
                driver.findElements(By.className("search-criteria-input")).get(1).clear();
                driver.findElements(By.className("search-criteria-input")).get(1).sendKeys("PLOS one");
                driver.findElements(By.className("select2-choice")).get(0).click();
                driver.findElement(By.id("select2-result-label-21")).click();
                driver.findElement(By.id("periodRange")).click();
                driver.findElement(By.id("s2id_autogen4")).click();
                System.out.println(driver.getPageSource());
                WebElement startYearEle = driver.findElement(By.name("startYear"));
                Select select = new Select(startYearEle);
                List<WebElement> options = select.getOptions();
                for (WebElement option : options) {
                    if (option.getAttribute("value").equals("2013"))
                        option.click();
                }
                WebElement endYear = driver.findElement(By.name("endYear"));
                select = new Select(endYear);
                options = select.getOptions();
                for (WebElement option : options) {
                    if (option.getAttribute("value").equals("2013"))
                        option.click();
                }
                driver.findElement(By.id("UA_GeneralSearch_input_form_sb")).click();
                break;
            } catch (Exception e) {

            }
        }
    }
}
